// Pawel Lebelt & Ania Janas
package application.PrintingModule;

import java.awt.Desktop;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.print.PrintException;

import application.client.data.Client;
import application.generateReports.DataToDateReport;
import application.generateReports.DateReport;
import application.generateReports.SingleReport;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPrintPage;


public class Printing 
{

	  public static PdfPTable createLabel(Client client,int parcelID) throws IOException, BadElementException {
	    	  
		    Font font = new Font();
	        font.setSize(20);
	        Font font2 = new Font();
	        font2.setStyle(2);
	        font2.setSize(20);
	        
	        PdfPTable table = new PdfPTable(2);
	        PdfPCell cell;
	        table.setWidthPercentage(100);
	        
	        Image image = Image.getInstance("img\\paczka.png");
			image.scalePercent(75);
			image.setAlignment(Element.ALIGN_CENTER);
	     
	        cell = new PdfPCell(new Phrase("ePaczka",font));
	        cell.addElement(image);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setColspan(2);
	        table.addCell(cell);
	        
	  
	        table.setTotalWidth(300);
	       
	        table.addCell(new PdfPCell(new Phrase("Id paczki",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(parcelID),font2)));
	        table.addCell(new PdfPCell(new Phrase("Imie",font)));
	        table.addCell(new PdfPCell(new Phrase(client.getFirstName(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Nazwisko",font)));
	        table.addCell(new PdfPCell(new Phrase(client.getLastName(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Ulica",font)));
	        table.addCell(new PdfPCell(new Phrase(client.getAddress().getStreet(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Nr domu",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(client.getAddress().getHouseNumber()),font2)));
	        table.addCell(new PdfPCell(new Phrase("Nr mieszkania",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(client.getAddress().getFlatNumber()),font2)));
	        table.addCell(new PdfPCell(new Phrase("Kod pocztowy",font)));
	        table.addCell(new PdfPCell(new Phrase(client.getAddress().getPostCode(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Miejscowosc",font)));
	        table.addCell(new PdfPCell(new Phrase(client.getAddress().getCity(),font2)));
	        
	        return table;
	    }
	  
	  public static PdfPTable createReportForSingleCustomer(SingleReport sReport) 
		{
		    Font font = new Font();
	        font.setSize(14);
	        
	        Font font2 = new Font();
	        font2.setSize(12);
	        font2.setStyle(3);
	        
	        PdfPTable table = new PdfPTable(2);
	        PdfPCell cell;
	        table.setWidthPercentage(95);
	        
	        cell = new PdfPCell(new Phrase("Raport o kliencie",font));
	        cell.setBorderWidth((float) 1.4);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setColspan(2);
	        cell.setBackgroundColor(BaseColor.ORANGE);
	        table.addCell(cell);
	       
	        table.addCell(new PdfPCell(new Phrase("Data wygenerowania",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getCreationDate().toString(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Imie",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getC().getFirstName(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Nazwisko",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getC().getLastName(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Ulica",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getC().getAddress().getStreet(),font)));
	        table.addCell(new PdfPCell(new Phrase("Nr domu",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(sReport.getDataToReport().getC().getAddress().getHouseNumber()),font)));
	        table.addCell(new PdfPCell(new Phrase("Nr mieszkania",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(sReport.getDataToReport().getC().getAddress().getFlatNumber()),font)));
	        table.addCell(new PdfPCell(new Phrase("Kod pocztowy",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getC().getAddress().getPostCode(),font)));
	        table.addCell(new PdfPCell(new Phrase("Miejscowosc",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getC().getAddress().getCity(),font)));
	        table.addCell(new PdfPCell(new Phrase("Liczba nadanych paczek",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(sReport.getDataToReport().getParcelsPosted()),font)));
	        table.addCell(new PdfPCell(new Phrase("Liczba otrzymanych paczek",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(sReport.getDataToReport().getParcelsReceived()),font)));
	        table.addCell(new PdfPCell(new Phrase("Data nadania ostatniej paczki",font)));
	        table.addCell(new PdfPCell(new Phrase(sReport.getDataToReport().getLastparcelsPosted(),font2)));
	        table.addCell(new PdfPCell(new Phrase("Laczny koszt nadanych paczek",font)));
	        table.addCell(new PdfPCell(new Phrase(Integer.toString(sReport.getDataToReport().getParcelsCost()),font)));
	    	
	        cell = new PdfPCell(new Phrase("<<<Wygenerowane przez system ePaczka>>>",font));
	        cell.setBorderWidth((float) 1.4);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setColspan(2);
	        cell.setBackgroundColor(BaseColor.ORANGE);
	        table.addCell(cell);
	        return table;       
		}
	  
	  	public static void generateDailyReport(String filename, DateReport dReport) throws FileNotFoundException, DocumentException 
		{
	  		Document document = new Document(PageSize.A4);
	        PdfWriter.getInstance(document, new FileOutputStream(filename));
	        document.open();  
	        
	        Font font = new Font();
	        font.setSize(12);
	        font.setStyle(3);
	        
	        PdfPTable tableTop = new PdfPTable(2);
        	tableTop.setWidthPercentage(95);
        	PdfPCell cellTop;
        	cellTop = new PdfPCell(new Phrase("Raport dzienny",font));
 	        cellTop.setBorderWidth((float) 1.4);
 	        cellTop.setHorizontalAlignment(Element.ALIGN_CENTER);
 	        cellTop.setColspan(2);
 	        tableTop.addCell(cellTop);
        	tableTop.addCell(new PdfPCell(new Phrase("Data wygenerowania",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(dReport.getCreationDate().toString(),font)));
	        tableTop.addCell(new PdfPCell(new Phrase("Godzina wygenerowania",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(dReport.getCreationTime().toString(),font)));
	        tableTop.addCell(new PdfPCell(new Phrase("Liczba nadanych paczek",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(Integer.toString(dReport.getDataToReport().size()),font)));
	        document.add(tableTop);
	        document.add(new Paragraph("\n"));
	        
	        font.setStyle(0);
	        
	        int listSize = dReport.getDataToReport().size();
	        
	        for (int i=0;i<listSize;i++)
	        {
	        	DataToDateReport tmp = dReport.getDataToReport().get(i);
	        	
	        	PdfPTable table = new PdfPTable(2);
	        	table.setWidthPercentage(95);
	        	PdfPCell cell,cell2;
	        	cell2 = new PdfPCell(new Phrase("---"+Integer.toString(i+1)+"---",font));
	        	cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
	        	cell2.setColspan(2);
	        	table.addCell(cell2);
	        	cell = new PdfPCell(new Phrase("Dane paczki",font));
	 	        cell.setBorderWidth((float) 1.4);
	 	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	        cell.setColspan(2);
	 	        cell.setBackgroundColor(BaseColor.ORANGE);
	 	        table.addCell(cell);
	        	table.addCell(new PdfPCell(new Phrase("Id paczki",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getParcelID()),font)));
		        table.addCell(new PdfPCell(new Phrase("Data nadania",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getP().getDate(),font)));
		        table.addCell(new PdfPCell(new Phrase("Cena",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getPrice()),font)));
		        table.addCell(new PdfPCell(new Phrase("Masa",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getMass()),font)));
		      
		        cell = new PdfPCell(new Phrase("Dane odbiorcy",font));
	 	        cell.setBorderWidth((float) 1.4);
	 	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	        cell.setColspan(2);
	 	        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
	 	        table.addCell(cell);
		        table.addCell(new PdfPCell(new Phrase("Imie",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getFirstName(),font)));
		        table.addCell(new PdfPCell(new Phrase("Nazwisko",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getLastName(),font)));
		        table.addCell(new PdfPCell(new Phrase("Ulica",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getStreet(),font)));
		        table.addCell(new PdfPCell(new Phrase("Nr domu",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getC().getAddress().getHouseNumber()),font)));
		        table.addCell(new PdfPCell(new Phrase("Nr mieszkania",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getC().getAddress().getFlatNumber()),font)));
		        table.addCell(new PdfPCell(new Phrase("Miasto",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getCity(),font)));
		        table.addCell(new PdfPCell(new Phrase("Kod pocztowy",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getPostCode(),font)));
		        
		        document.add(table);
		        document.add(new Paragraph(" "));
	        }
	        
	        document.close();
	         
		}
	  
	  	public void generateLabel(String filename, Client client,int parcelID)throws DocumentException, IOException 
	  	{
			    
			        Document document = new Document(PageSize.B6.rotate());
			        PdfWriter.getInstance(document, new FileOutputStream(filename));
			        document.open();  
			        document.add(createLabel(client,parcelID));
			        document.close();
	  	}
		
		public void generateReportForSingleCustomer(String filename, SingleReport sReport) throws DocumentException, IOException  
		{
			    Document document = new Document(PageSize.A4);
		        PdfWriter.getInstance(document, new FileOutputStream(filename));
		        document.open();  
		        document.add(createReportForSingleCustomer(sReport));
		        document.close();
		}
		
	  	public static void generatePeriodicReport(String filename, DateReport fReport) throws FileNotFoundException, DocumentException 
		{
	  		Document document = new Document(PageSize.A4);
	        PdfWriter.getInstance(document, new FileOutputStream(filename));
	        document.open();  
	        
	        Font font = new Font();
	        font.setSize(12);
	        font.setStyle(3);
	        
	        PdfPTable tableTop = new PdfPTable(2);
        	tableTop.setWidthPercentage(95);
        	PdfPCell cellTop;
        	cellTop = new PdfPCell(new Phrase("Raport okresowy",font));
 	        cellTop.setBorderWidth((float) 1.4);
 	        cellTop.setHorizontalAlignment(Element.ALIGN_CENTER);
 	        cellTop.setColspan(2);
 	        tableTop.addCell(cellTop);
        	tableTop.addCell(new PdfPCell(new Phrase("Data wygenerowania",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(fReport.getCreationDate().toString(),font)));
	        tableTop.addCell(new PdfPCell(new Phrase("Godzina wygenerowania",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(fReport.getCreationTime().toString(),font)));
	        tableTop.addCell(new PdfPCell(new Phrase("Liczba nadanych paczek",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(Integer.toString(fReport.getDataToReport().size()),font)));
	        tableTop.addCell(new PdfPCell(new Phrase("Okres",font)));
	        tableTop.addCell(new PdfPCell(new Phrase(fReport.getReportName(),font)));
	        
	        document.add(tableTop);
	        document.add(new Paragraph("\n"));
	        
	        font.setStyle(0);
	        
	        int listSize = fReport.getDataToReport().size();
	        
	        for (int i=0;i<listSize;i++)
	        {
	        	DataToDateReport tmp = fReport.getDataToReport().get(i);
	        	
	        	PdfPTable table = new PdfPTable(2);
	        	table.setWidthPercentage(95);
	        	PdfPCell cell,cell2;
	        	cell2 = new PdfPCell(new Phrase("---"+Integer.toString(i+1)+"---",font));
	        	cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
	        	cell2.setColspan(2);
	        	table.addCell(cell2);
	        	cell = new PdfPCell(new Phrase("Dane paczki",font));
	 	        cell.setBorderWidth((float) 1.4);
	 	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	        cell.setColspan(2);
	 	        cell.setBackgroundColor(BaseColor.ORANGE);
	 	        table.addCell(cell);
	        	table.addCell(new PdfPCell(new Phrase("Id paczki",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getParcelID()),font)));
		        table.addCell(new PdfPCell(new Phrase("Data nadania",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getP().getDate(),font)));
		        table.addCell(new PdfPCell(new Phrase("Cena",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getPrice()),font)));
		        table.addCell(new PdfPCell(new Phrase("Masa",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getP().getMass()),font)));
		      
		        cell = new PdfPCell(new Phrase("Dane odbiorcy",font));
	 	        cell.setBorderWidth((float) 1.4);
	 	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	        cell.setColspan(2);
	 	        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
	 	        table.addCell(cell);
		        table.addCell(new PdfPCell(new Phrase("Imie",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getFirstName(),font)));
		        table.addCell(new PdfPCell(new Phrase("Nazwisko",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getLastName(),font)));
		        table.addCell(new PdfPCell(new Phrase("Ulica",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getStreet(),font)));
		        table.addCell(new PdfPCell(new Phrase("Nr domu",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getC().getAddress().getHouseNumber()),font)));
		        table.addCell(new PdfPCell(new Phrase("Nr mieszkania",font)));
		        table.addCell(new PdfPCell(new Phrase(Integer.toString(tmp.getC().getAddress().getFlatNumber()),font)));
		        table.addCell(new PdfPCell(new Phrase("Miasto",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getCity(),font)));
		        table.addCell(new PdfPCell(new Phrase("Kod pocztowy",font)));
		        table.addCell(new PdfPCell(new Phrase(tmp.getC().getAddress().getPostCode(),font)));
		        
		        document.add(table);
		        document.add(new Paragraph(" "));
	        }
	        
	        document.close();
	         
		}
				
	  public boolean openPDF(String path) 
	  {
	        Desktop pdf = java.awt.Desktop.getDesktop();
	        try 
	        {
	            pdf.open(new java.io.File(path));
	            return true;
	        } 
	        catch (IOException ex) {
	           return false;
	        }
	  }
	  
	  public void printDocument(String path)throws PrintException, IOException, PrinterException
	  {
	  	//Renderowanie pdfa z pliku źródłowego
	  	File f = new File(path);
	  	FileInputStream fis = new FileInputStream(f);
	  	FileChannel fc = fis.getChannel();
	  	ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
	  	PDFFile pdfFile = new PDFFile(bb);
	  	PDFPrintPage pages = new PDFPrintPage(pdfFile);
	  	 
	  	//Uruchomienie drukowania
	  	PrinterJob pjob = PrinterJob.getPrinterJob();
	  	PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
	  	pjob.setJobName(f.getName());
	  	Book book = new Book();
	  	book.append(pages, pf, pdfFile.getNumPages());
	  	pjob.setPageable(book);
	  	 
	  	//Wysłanie na drukarkę
	  	pjob.print();


	  }
	 /* alternatywane drukowanie za pomoca acrobat readera (trzeba dodac com.lowagie.text_
	  
	  public void callExcecutablePrint(String fileName){
		     try{
		             Executable ex = new Executable();
		            // ex.openDocument(fileName);
		             ex.printDocument(fileName);
		     }catch(IOException e){
		             e.printStackTrace();
		     }
		} */
}

		
	 
	 
	 
